<?php
namespace Index\Controller;

use Index\Base\BaseController;

class IndexController extends BaseController
{

    public function test(){
        $url = "http://www.iqiyi.com/v_19rrf6eqrk.html?fc=82992814760eeac6";
        $data = file_get_contents($url);

        // 取出视频所需key
        preg_match('/("videoId":"(.*)")|(data-player-videoid="(.*)")/U', $data, $result);

        if (isset($result[4]))
        {
            echo "http://www.iqiyi.com/player/20130315154043/SharePlayer.swf?vid={$result[4]}";
        }
    }

    public function uploadImg(){
        $dir = "./Upload/";
        $a = scandir($dir);
        //先获取分页条，根据分页条的规律来下载图片
        Vendor('phpQuery.phpQuery');
        $imgArr = array();
        for($j = 1;$j<=50;$j++){
            $page_url = 'http://list.iqiyi.com/www/1/-------------11-'.$j.'-1-iqiyi--.html';
            \phpQuery::newDocumentFile($page_url);
            $pageAll = pq('.site-piclist-auto li')->html();
            preg_match_all('| src="([^"]*).jpg"|', $pageAll, $i);//视频封面

            foreach($i[1] as $item){
                $y_url = $item;
                $url = '';
                if(strpos($y_url,'http')){
                    $url = $item.'.jpg';
                }else{
                    $url = 'http:'.$item.'.jpg';
                }

                $a=strrchr($url,'/');
                $b=explode('/',$a);
                $name = $b[1];

                if(array_search($name,$a)){
                   break;
                }else{
                    getImage($url,'./Upload/',$name);
                    array_push($imgArr,$name);
                }
            }
        }
        var_dump($imgArr);
    }

    //爱奇艺
    public function index()
    {
        $dir = "./Upload/";
        $dir_arr = scandir($dir);
        Vendor('phpQuery.phpQuery');
        $url = $_GET['url'];
        $ti =  $_GET['u'];
        if ($ti == "") $ti = '电影';
        if ($url == "") {
            //            $url = "http://www.soku.com/search_video/q_%E9%9D%92%E4%BA%91%E5%BF%97";
            $url = 'http://list.iqiyi.com/www/1/-------------11-1-1-iqiyi--.html';
        } else {

            $url = 'http://list.iqiyi.com' . $url;
        }

        \phpQuery::newDocumentFile($url);
        $pageTool = str_replace('mod-page', 'yk-pager', pq('.mod-page'));
        $pageTool = str_replace('/www/', U('index').'?url=/www/', $pageTool);

        $pageAll = pq('.site-piclist-auto li')->html();


        preg_match_all('| src="([^"]*).jpg"|', $pageAll, $i);//视频封面

//        $imgList = array();
//        foreach ($pageAll as $li) {
//            $item = pq($li)->find('img:eq(0)')->attr('src');
//            array_push($imgList,$item);
//        }

        //先清除upload文件下的文件

//        $dir = "Upload/"; // 文件夹的名称
//        if (is_dir($dir)){
//            if ($dh = opendir($dir)){
//                while (($file = readdir($dh)) !== false){
//                    list($filesname,$kzm)=explode(".",$file);//获取扩展名
//                    if($kzm=="gif" or $kzm=="jpg" or $kzm=="JPG") { //文件过滤
//                        if(unlink('Upload/'.$file)){
//
//                        };
//                    }
//
//                }
//                closedir($dh);
//            }
//        }

        //批量下载图片（为了防止盗链图片把图片先下下来）


        $imgArr = array();
        foreach($i[1] as $item){

            $y_url = $item;

            $url = '';
            if(strpos($y_url,'http')){
                $url = $item.'.jpg';
            }else{
                $url = 'http:'.$item.'.jpg';
            }


            $a=strrchr($url,'/');
            $b=explode('/',$a);
            $name = $b[1];

            //判断name是否在$a数组中 ，没有再下；
            if(array_search($name,$dir_arr)){
                array_push($imgArr,$name);
            }else{
                getImage($url,'./Upload/',$name);
                array_push($imgArr,$name);
            }

        }



        $this->assign("list3", $imgArr);

        $roleList = pq('.role_info');
        $roleH = array();
        foreach ($roleList as $li) {
            $item = pq($li)->html();
            array_push($roleH, $item);
        }
        $this->assign("list4", $roleH);

        $pageA = pq('.site-piclist_pic a');
        $urlList = array();
        $altList = array();
        foreach ($pageA as $li) {

            $item = pq($li)->attr('href');
            $item = str_replace('http://www.iqiyi.com/', U('read2').'?url=/', $item);
            array_push($urlList, $item);
            $alt = pq($li)->attr('alt');
            array_push($altList, $alt);

        }
        $this->assign("list2", $urlList);
        $this->assign("list", $altList);


        $this->assign("ti", $ti);//标题
        $this->assign("page", $pageTool);//分页条

        $this->common_display();
    }


    //优酷
    public function vod()
    {

        $url = $_GET['url'];
        $ti =  $_GET['u'];
        if ($ti == "") $ti = '电影';

        if ($url == "") {
            //            $url = "http://www.soku.com/search_video/q_%E9%9D%92%E4%BA%91%E5%BF%97";
            $url = 'http://list.youku.com/category/show/c_96_pt_0_s_1_d_1.html?spm=a2h1n.8251845';
        } else {

            $url = 'http://list.youku.com' . $url;
        }

        //  echo $url;die;
        //拿去整个页面
        $str = xurl($url);

        //切出视频列表和分页条
        $page = nanji_cut("<div class=\"box-series\">", "</div><div class=\"vault-banner mb20\">", $str);

        //切出分页条
        $pageTool = nanji_cut("<div class=\"yk-pager\">", "</div>", $page);

        //$page=str_replace('<ul class="yk-pages">','<ul class="pagination">',$page);

        //
        $pageTool = str_replace('//list.youku.com', U('vod').'?url=', $pageTool);


        $str = str_replace('//v.youku.com/v_show/', U('read').'?url=', $str);


        preg_match_all('|<div class="p-thumb"><a href="([^"]*)" title="([^"]*)"([^"]*)|', $str, $a);//把当页的视频列表放入a集合

        preg_match_all('|<img class="quic" _src="([^"]*)"|', $str, $i);//视频封面

        preg_match_all('|</li><li>([^"]*) </li></ul>|', $str, $i2);//播放次数

        if ($a == '') echo '数据为空！';


        $this->assign("list3", $i[1]);//30个视频封面
        $this->assign("list4", $i2[1]);//30个播放次数
        $this->assign("list2", $a[1]);//30个链接地址
        $this->assign("list", $a[2]);//30个视频名称
        $this->assign("ti", $ti);//标题
        $this->assign("page", $pageTool);//分页条
        $this->common_display();
    }


    public function read()
    {

        $url = $_GET['url'];
        $yurl = 'http://v.youku.com/'.$url;
//        $arr = explode('_', $url);
//        $arr1 = explode('.', $arr[1]);
//        $uurl = 'http://player.youku.com/embed/' . $arr1[0];
       // $nurl = 'http://v.buy360.vip/cxjx.php?v='.$yurl;

        $nurl = 'http://vip.hfj666.cn/player.php?url='.$yurl;
        $this->assign('uurl', $nurl);

        $this->common_display();
    }

    public function read2()
    {

        $url = $_GET['url'];
        $yurl = 'http://www.iqiyi.com'.$url;

//        $arr = explode('_', $url);
//        $arr1 = explode('.', $arr[1]);
//        $uurl = 'http://player.youku.com/embed/' . $arr1[0];
//        $nurl = 'http://api.baiyug.cn/vip/index.php?url='.$yurl;
        $nurl = 'http://vip.hfj666.cn/player.php?url='.$yurl;

        $this->assign('uurl', $nurl);

        $this->common_display();
    }

    public function read_all()
    {

        $url = $_GET['url'];
        $uurl = 'http://vip.hfj666.cn/player.php?url='.$url;

        $this->assign('uurl', $uurl);

        $this->common_display();
    }

    public function search(){
        $name = $_POST['wd'];

        $search_url = 'http://so.iqiyi.com/so/q_'.urlencode($name);
        //从页面中获取名称和主演列表、图片、链接
        Vendor('phpQuery.phpQuery');
        \phpQuery::newDocumentFile($search_url);
        $pageA = pq('.mod_result_list li');
        $pageL =array();

        foreach ($pageA as $li) {
            $item = pq($li)->html();
            array_push($pageL, $item);
        }
        preg_match_all('| href="([^"]*)" data-playsrc-linktype="play"|', $pageL[0], $a_list);
        $href_url = $a_list[1][0];//链接

        preg_match_all('| title="([^"]*)" src="([^"]*)">|', $pageL[0], $b_list);
        $title_url = $b_list[1][0];//名称
        $img_url = $b_list[2][0];//图片路径
        //判断图片是否有，没有就下载；


        $dir = "./Upload/";
        $dir_arr = scandir($dir);
        $y_url = $img_url;

        $c_url = '';
        if(strpos($y_url,'http')){
            $c_url = $y_url.'.jpg';
        }else{
            $c_url = 'http:'.$y_url;
        }


        $a_test=strrchr($c_url,'/');
        $b_test=explode('/',$a_test);
        $name_url = $b_test[1];

        //判断name是否在$a数组中 ，没有再下；
        if(array_search($name_url,$dir_arr)){

        }else{
            getImage($c_url,'./Upload/',$name_url);

        }
        $this->assign('title_url', $title_url);
        $new_url = U('read_all').'?url='.$href_url;
        $this->assign('href_url', $new_url);
        $this->assign('img_url', $name_url);
        $this->common_display();
    }

    public function parse(){
        $this->common_display();
    }

    public function admire(){
        $this->common_display();
    }


}